# coding=utf-8

from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.utils import simplejson
from django.db.models import Q

from openpolitics.models import Person, Deputy, Position, Term, DataType

def query(request,
          template_name='openpolitics/api'):
    '''
    Entry point for queries
    '''
    party = request.GET.get('party','')
    if party:
        return party_query(request, template_name)
    inst = request.GET.get('institution','')
    if inst:
        return inst_query(request, template_name)

    query = request.GET.get('query','')
    if query:
        return free_query(request, template_name)
    return HttpResponse('Wybierz zapytanie. Na przykład: <a href="?query=Kom">?query=Kom</a>, <a href="?party=PiS">?party=PiS</a> lub or <a href="?institution=Senat">?institution=Senat</a>')

def free_query(request,
          template_name='openpolitics/api'):
    '''
    Query for a list of people
    '''
    query = request.GET.get('query','')
    people = Person.objects.filter(Q(first_name__icontains=query) | Q(last_name__icontains=query))
    items = []
    for person in people:
        try:
            party = person.current_party.shortname
        except:
            party = None
        try:
            www = person.data.get(type__name='source_sejmgovpl_www').value
        except:
            www = None
        pitem = {'first_name': person.first_name,
                 'last_name': person.last_name,
                 'email': person.data.get(type__name='source_sejmgovpl_email').value,
                 'www': www,
                 'party': party}
        items.append(pitem)
    return HttpResponse(simplejson.dumps({'items': items}, indent=2))

def party_query(request,
          template_name='openpolitics/api'):
    '''
    Query for party members
    '''
    party = request.GET.get('party','')
    people = Deputy.objects.filter(positions__institution__shortname=party)
    items = []
    for person in people:
        try:
            party = person.party_affiliation.shortname
        except:
            party = None
        try:
            email = person.data.get(type__name='source_sejmgovpl_email').value
        except:
            try:
                email = person.data.get(type__name='source_senatgovpl_email').value
            except:
                email = None
        pitem = {'first_name': person.first_name,
                 'last_name': person.last_name,
                 'email': email,
                 'party': party}
        items.append(pitem)
    return HttpResponse(simplejson.dumps({'items': items}, indent=2))

def inst_query(request,
          template_name='openpolitics/api'):
    '''
    Query for party members
    '''
    inst = request.GET.get('institution','')
    people = Deputy.objects.filter(positions__institution__shortname=inst)
    items = []
    for person in people:
        try:
            party = person.party_affiliation.shortname
        except:
            party = None
        try:
            email = person.data.get(type__name='source_sejmgovpl_email').value
        except:
            try:
                email = person.data.get(type__name='source_senatgovpl_email').value
            except:
                email = None
        pitem = {'first_name': person.first_name,
                 'last_name': person.last_name,
                 'email': email,
                 'party': party}
        items.append(pitem)
    return HttpResponse(simplejson.dumps({'items': items}, indent=2))
